package cn.fresh.xuanyuan.module.product.service.orchard;

import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.fresh.xuanyuan.module.product.dal.dataobject.orchard.OrchardDO;
import cn.fresh.xuanyuan.module.product.dal.mysql.category.ProductCategoryMapper;
import cn.fresh.xuanyuan.module.product.dal.mysql.orchard.OrchardMapper;
import cn.fresh.xuanyuan.module.product.vo.orchard.OrchardRespVO;
import cn.fresh.xuanyuan.module.product.vo.orchard.OrchardSaveReqVO;
import cn.fresh.xuanyuan.module.product.vo.orchard.OrchardVO;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Validated
public class OrchardServiceImpl implements OrchardService {

    @Autowired
    private OrchardMapper orchardMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public PageResult<OrchardDO> getOrchardPage(String name, String phone, PageParam pageReq) {
        return orchardMapper.getOrchardPage(name, phone, pageReq);
    }

    @Override
    public void save(OrchardSaveReqVO saveReqVO) {
        OrchardDO orchardDO = new OrchardDO();
        BeanUtils.copyProperties(saveReqVO, orchardDO);
        orchardDO.setCreateTime(LocalDateTime.now());
        if(saveReqVO.getCateIds() != null && !saveReqVO.getCateIds().isEmpty()) {
            List<ProductCategoryDO> categoryDOList = productCategoryMapper.getCategoryByIds(saveReqVO.getCateIds());
            if(!CollectionUtil.isEmpty(categoryDOList)) {
                List<String> categoryNameList = new ArrayList<>();
                categoryDOList.forEach(categoryDO -> {
                    categoryNameList.add(categoryDO.getName());
                });
                orchardDO.setCategoryName(JSON.toJSONString(categoryNameList));
                orchardDO.setCategoryId(JSON.toJSONString(saveReqVO.getCateIds()));
            }
        }
        orchardMapper.insert(orchardDO);
    }

    @Override
    public void edit(OrchardSaveReqVO saveReqVO) {
        OrchardDO orchardDO = new OrchardDO();
        BeanUtils.copyProperties(saveReqVO, orchardDO);
        orchardDO.setUpdateTime(LocalDateTime.now());
        if(saveReqVO.getCateIds() != null && !saveReqVO.getCateIds().isEmpty()) {
            List<ProductCategoryDO> categoryDOList = productCategoryMapper.getCategoryByIds(saveReqVO.getCateIds());
            if(!CollectionUtil.isEmpty(categoryDOList)) {
                List<String> categoryNameList = new ArrayList<>();
                categoryDOList.forEach(categoryDO -> {
                    categoryNameList.add(categoryDO.getName());
                });
                orchardDO.setCategoryName(JSON.toJSONString(categoryNameList));
                orchardDO.setCategoryId(JSON.toJSONString(saveReqVO.getCateIds()));
            }
        }
        orchardMapper.updateById(orchardDO);
    }

    @Override
    public List<OrchardRespVO> getAll() {
        LambdaQueryWrapper<OrchardDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrchardDO::getStatus, true);
        List<OrchardDO> list = orchardMapper.selectList(queryWrapper);
        List<OrchardRespVO> respVOList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for(OrchardDO orchardDO : list) {
                OrchardRespVO orchardRespVO = new OrchardRespVO();
                orchardRespVO.setId(orchardDO.getId());
                orchardRespVO.setName(orchardDO.getName());
                respVOList.add(orchardRespVO);
            }
        }
        return respVOList;
    }

    @Override
    public OrchardRespVO getDetailById(Integer id) {
        OrchardDO orchardDO = orchardMapper.selectById(id);
        if(orchardDO != null) {
            OrchardRespVO orchardRespVO = new OrchardRespVO();
            BeanUtils.copyProperties(orchardDO, orchardRespVO);
            orchardRespVO.setCateIds(JSON.parseArray(orchardDO.getCategoryId(), Long.class));
            return orchardRespVO;
        }
        return null;
    }

    @Override
    public List<OrchardVO> getOrchardList(PageParam pageReq) {
        List<OrchardVO> list = new ArrayList<>();
        PageResult<OrchardDO> page = orchardMapper.getOrchardPage(null, null, pageReq);
        if(page != null && CollectionUtil.isNotEmpty(page.getList())) {
            List<OrchardDO> queryList = page.getList();
            queryList.forEach(o -> {
                OrchardVO vo = new OrchardVO();
                BeanUtils.copyProperties(o,vo);
                list.add(vo);
            });
        }
        return list;
    }
}
